<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class LGSliderWidget extends CWidget {

    /**
     *
     * @var array<SlideItem> 
     */
    public $slideItems;
    public $id;
    public $htmlOptions = array();

    public function init() {
	parent::init();

	if (!isset($this->slideItems)) {
	    throw new CException(Yii::t('zii', 'The "slideItems" property cannot be empty.'));
	}

	if (isset($this->id)) {
	    $this->htmlOptions['id'] = $this->id;
	}
    }

    public function run() {
	parent::run();

	$publishedSlideNumber = 0;
	
	foreach ($this->slideItems as $slideItem) {
	    if ($slideItem->status == LGConstant::PUBLISH_STATUS) {
		$publishedSlideNumber++;
	    }
	}

	if ($publishedSlideNumber == 1) {
	    
	    foreach ($this->slideItems as $slideItem) {
	    if ($slideItem->status == LGConstant::PUBLISH_STATUS) {
		$this->renderSingleSlide($slideItem);
	    }
	}
	
	    
	} else {
//	    $assets = dirname(__FILE__) . '/assets';
//	    $baseUrl = Yii::app()->assetManager->publish($assets);
//	     if (is_dir($assets)) {
//	    Yii::app()->getClientScript()->registerScriptFile(
//		    $baseUrl . '/loopedslider.js', CClientScript::POS_HEAD);
//
//	     }
	    $this->renderSilders($this->slideItems);
	}
    }

    /**
     * 
     * @param SlideItem $slideItem
     */
    public function renderSilders($slideItems) {
	return $this->render('_sliderView', array('slideItems' => $slideItems));
    }

    public function renderSingleSlide($slideItem) {
	return $this->render('_singleSliderView', array('slideItem' => $slideItem));
    }

}

?>
